package com.room.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cyh.dao.SysConstant;
import com.cyh.dao.dto.RoomDto;
import com.cyh.dao.dto.SolicitDmtDto;
import com.cyh.dao.entity.Room;
import com.cyh.dao.entity.SolicitDmt;
import com.cyh.dao.entity.SolicitNew;
import com.cyh.dao.mapper.RoomMapper;
import com.cyh.dao.mapper.SolicitDmtMapper;
import com.cyh.dao.mapper.SolicitNewMapper;
import com.room.core.util.NumberUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class ManageService {

    @Autowired
    private RoomService roomService;

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private SolicitDmtService solicitDmtService;

    @Autowired
    private SolicitDmtMapper solicitDmtMapper;

    @Autowired
    private SolicitNewMapper solicitNewMapper;

    @Autowired
    private SolicitNewService solicitNewService;

    @Transactional(rollbackFor = Exception.class)
    public void addRoom(RoomDto dto) {
        Room room = new Room();
        BeanUtils.copyProperties(dto, room);
        room.setLevel(SysConstant.ROOM_LEVEL_ROOM);
        roomService.save(room);

        List<Room> list = new ArrayList<>();
        for (int i=1; i<=dto.getNum(); i++){
            Room r = Room.builder()
                    .paterId(room.getId())
                    .dmtIs(room.getDmtIs())
                    .level(SysConstant.ROOM_LEVEL_SEAT)
                    .name(NumberUtil.getSerialNumber(i))
                    .build();
            list.add(r);
        }
        roomService.saveBatch(list);
    }

    public List<RoomDto> getTimeIntervalList(Long timeInterval) {
        List<RoomDto> list = roomMapper.getTimeIntervalList(timeInterval);
        return list;
    }

    public void removeTimeInterval(Integer timeInterval, Long id) {
        LambdaUpdateWrapper<Room> wrapper = new UpdateWrapper<Room>()
                .lambda().eq(Room::getId, id);
        if (SysConstant.SJD_A.equals(timeInterval)){
            wrapper.set(Room::getSjdA,0);
        }else if (SysConstant.SJD_B.equals(timeInterval)){
            wrapper.set(Room::getSjdB,0);
        }else if(SysConstant.SJD_C.equals(timeInterval)) {
            wrapper.set(Room::getSjdC, 0);
        }
        roomService.update(new Room(), wrapper);
    }

    public List<SolicitDmtDto> getSolicitDmtList() {
        List<SolicitDmtDto> list = solicitDmtMapper.getSolicitDmtList(null);
        return list;
    }

    public SolicitDmtDto getSolicitDmtDetail(Long id) {
        List<SolicitDmtDto> solicitDmtList = solicitDmtMapper.getSolicitDmtList(id);
        if (CollectionUtils.isEmpty(solicitDmtList)){
            return null;
        }else{
            SolicitDmtDto solicitDmtDto = solicitDmtList.get(0);
            solicitDmtDto.setTimeIntervalFormat(
                    SysConstant.getValue("timeInterval",
                            solicitDmtDto.getTimeInterval()));
            return solicitDmtDto;
        }
    }

    public void reviewSolicitDmt(Long id, Integer agreeIs) {
        solicitDmtService.update(new SolicitDmt(),new UpdateWrapper<SolicitDmt>().lambda().
                eq(SolicitDmt::getId,id).set(SolicitDmt::getAgreeIs, agreeIs));
    }

    public Integer getSolicitNew() {
        return solicitNewMapper.getSolicitNew();
    }

    public void reviewSolicitNew(Integer state) {
        solicitNewService.update(new SolicitNew(),new UpdateWrapper<SolicitNew>().lambda().
                eq(SolicitNew::getAgreeIs, 0).set(SolicitNew::getAgreeIs, state));
    }

    public List<RoomDto> getNeedTimeIntervalList(Integer timeInterval) {
        List<RoomDto> list = roomMapper.getNeedTimeIntervalList(timeInterval);
        return list;
    }

    public void bindTimeIntervalRoom(Integer timeInterval, Long roomId) {
        Room room = Room.builder().id(roomId).build();
        if (timeInterval.equals(SysConstant.SJD_A)){
            room.setSjdA(1);
        }else if(timeInterval.equals(SysConstant.SJD_B)){
            room.setSjdB(1);
        }else if(timeInterval.equals(SysConstant.SJD_C)){
            room.setSjdC(1);
        }
        roomService.updateById(room);
    }
}
